package xcu.lxj.nettychat.chat.consumer;

import lombok.AllArgsConstructor;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xcu.lxj.nettychat.chat.constant.MQConstant;
import xcu.lxj.nettychat.chat.dao.*;
import xcu.lxj.nettychat.chat.domain.dto.MsgSendMessageDTO;
import xcu.lxj.nettychat.chat.domain.entity.Message;
import xcu.lxj.nettychat.chat.domain.entity.Room;
import xcu.lxj.nettychat.chat.domain.entity.RoomFriend;
import xcu.lxj.nettychat.chat.domain.entity.RoomGroup;
import xcu.lxj.nettychat.chat.domain.enums.RoomTypeEnum;
import xcu.lxj.nettychat.chat.domain.vo.response.ChatMsgResp;
import xcu.lxj.nettychat.chat.service.adapter.WSAdapter;
import xcu.lxj.nettychat.chat.service.impl.PushService;
import xcu.lxj.nettychat.chat.service.inter.ChatService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 *
 * @Author XinJian Lei
 * @Date 2024/9/26 16:33
 * @Version 1.0
 */
@RocketMQMessageListener(consumerGroup = MQConstant.SEND_MSG_GROUP, topic = MQConstant.SEND_MSG_TOPIC)
@Component
@AllArgsConstructor

public class MsgSendConsumer implements RocketMQListener<MsgSendMessageDTO> {


    @Autowired
    private  MessageDao messageDao;
    @Autowired
    private RoomFriendDao roomFriendDao;
    @Autowired
    RoomDao roomDao;
    @Autowired
    private GroupMemberDao groupMemberDao;
    @Autowired
    private RoomGroupDao roomGroupDao;
    @Autowired
    private PushService pushService;
    @Autowired
    private ChatService chatService;
    @Autowired
    private ContactDao contactDao;



    @Override
    public void onMessage(MsgSendMessageDTO msg) {
        // 拿到这个消息
        Message message = messageDao.getById(msg.getMsgId());
        // 拿到对应的房间
        Room room = roomDao.getById(message.getRoomId());
        // 拿到消息响应体
        ChatMsgResp msgResp = chatService.getMsgResp(message, null);
        // 获取房间类型
        Integer roomType = room.getType();
        List<Long> memberUidList = new ArrayList<>();
        // 更新房间的 活动时间
        roomDao.refreshActiveTime(room.getId(), message.getId(), message.getCreateTime());
        /**
         * 判断 消息发送的类型 拿到对应的 成员
         */
        if(Objects.equals(roomType, RoomTypeEnum.GROUP.getType())){
            // 拿到群的信息
            RoomGroup group = roomGroupDao.getByRoomId(room.getId());
            // 拿到 群成员 全部 uid
            memberUidList = groupMemberDao.getMemberUidList(group.getId());
        } else if(Objects.equals(roomType,RoomTypeEnum.FRIEND.getType())){
            // 通过 房间id 获得 房间里面的人
            RoomFriend roomFriend = roomFriendDao.getByRoomId(room.getId());
            memberUidList = Arrays.asList(roomFriend.getUid1(),roomFriend.getUid2());
        }
        //更新所有成员的会话时间
        contactDao.refreshOrCreateActiveTime(room.getId(), memberUidList, message.getId(), message.getCreateTime());

        //推送 给房间的成员
        pushService.sendPushMsg(WSAdapter.buildMsgSend(msgResp),memberUidList);


    }


}
